define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config;
    require("jquery_ui");
    require("./uploadify");

    var uploadify = function(element, options) {
        this.$element = element;
        this.options = $.extend(true, {}, uploadify.defaults, options);
        this.quantity = this.options.count || 1;
        this.Limit = this.options.count - this.Nums();
        var _self = this;
        this.$element.uploadify({
            swf: _self.options.swf_path,
            uploader: _self.options.upload_path,
            cancelImage: "uploadify-cancel.png",
            buttonClass: "btn btn-default ",
            removeTimeout: 0,
            fileSizeLimit: _self.options.maxlength,
            buttonText: '<i class="fa fa-plus m-r-xs"><\/i> 添加图片',
            formData: _self.options.data,
            buttonCursor: "pointer",
            fileTypeDesc: "图片格式",
            fileTypeExts: "*.jpg;*.bmp;*.png; *.jpeg",
            queueSizeLimit: 100,
            uploadLimit: _self.Limit || 1,
            onUploadError: function(n) {
                config.msg.info(n.name + "上传失败!");
            },
            onUploadStart: function() {
                _self.$element.uploadify('settings', 'buttonText', '<i class="fa fa-plus m-r-xs"><\/i>继续上传');
            },
            onInit: function() {
                if (_self.options.count - _self.Nums() <= 0) {
                    _self.$element.uploadify('disable', true);
                    _self.$element.uploadify('settings', 'buttonText', '上传已达限制...');
                }
            },
            onUploadSuccess: function(n, t) {
                var d = $.parseJSON(t);
                if (!config.issucceed(d)) {
                    config.msg.info(d.message || t);
                    return
                }
                // typeof G.logic.uploadify.Callback != "undefined" && G.logic.uploadify.Callback(i.image);
                _self.add(d.image)
            }
        });
        if (this.options.placeholder) {
            var must = this.options.must ? '<span class="maroon">*</span>' : '';
            $("#file_upload").append(must + this.options.placeholder);
        };


        $(document).on("click", "a.item_close", function(e) {
            var t = $(this);
            $.post(_self.options.delete_path, {
                id: t.data("postId"),
                url: t.data("path")
            });
            if (_self.options.count - _self.Nums() >= 0) {
                _self.$element.uploadify('disable', false);
                _self.$element.uploadify('settings', 'buttonText', '<i class="fa fa-plus m-r-xs"><\/i>继续上传');
            }
            var $box = $(e.target).closest("li.imgbox");
            $box.fadeOut(function() {
                $box.remove()
            });
        });
        $("ul.ipost-list").sortable({
            opacity: .8
        })

    }
    uploadify.defaults = {
        count: 20,
        data: null,
        upload_path: config.domain.www + "/api/form/uploadImg",
        delete_path: config.domain.www + "/api/form/removeImg",
        swf_path: config.domain.static + "/assets/swf/uploadify.swf",
        maxlength: "2MB",
        must: true,
        placeholder: '<span class="help-inline">图片大小不超过2MB</span>'
    };
    var tmp = '<li class="imgbox" data-post-id="{0}" data-path="{2}"><a class="item_close" href="javascript:void(0)" title="删除"><\/a>  ' +
        '<input type="hidden" value="{0}" name="GoodsImages[{3}][id]" /> <input type="hidden" value="{2}" name="GoodsImages[{3}][path]" /> <input type="hidden" value="{4}" name="GoodsImages[{3}][file_name]" /> ' +
        '<span class="item_box"><img src="{1}" /><\/span><\/li>';
    uploadify.prototype.add = function(data, count) {
        $("#fileList").append(tmp.format(data.id, data.thumbnail, data.path, this.Nums(),data.file_name));
        if (this.quantity - this.Nums() <= 0) {
            this.$element.uploadify('disable', true);
            this.$element.uploadify('settings', 'buttonText', '上传已达限制...');
        }
    }
    uploadify.prototype.Nums = function() {
        return $("li.imgbox").length;
    }

    var $uploadify = $('[data-toggle="uploadify"]');
    if ($uploadify.length > 0) {
        $.each($uploadify, function() {
            new uploadify($(this), window.upload_options)
        });
    };

})
